﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Enring.Model.Flow;
using Enring.Model.Range;
using Enring.Model.Global;
using System.Data.Objects;
using System.Web;

namespace Enring
{
    public partial class EnringContext
    {
        private static Configration.ConfigrationManager configrationManager;
        private static string rootPath;
        private static string EnringToken = "EnringToken";
        private static EnringContext encontext = null;
        private HttpContext context;

        
        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="path"></param>
        public static void Init(string path)
        {
            rootPath = path;
            
        }

        public bool IsLogin { get; set; }
        private User currentUser;
        public User CurrentUser
        {
            get 
            {
                if (currentUser == null)
                    currentUser = new User();
                return currentUser;
            }
            set
            {
                currentUser = value;
            }
        }

        public string CurrentUserID 
        {
            get 
            {
                if (HttpContext.Current.Session[Const.CurrentUserID] != null)
                    return HttpContext.Current.Session[Const.CurrentUserID].ToString();
                else
                    return string.Empty;
            }
        }

        /// <summary>
        /// Check the user has login
        /// </summary>
        /// <returns></returns>
        public bool HasLogin()
        {
            return HttpContext.Current.Session[Const.CurrentUserID] != null;
        }

        /// <summary>
        /// Check the user password and create a session object automaticly.
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <param name="rememberme"></param>
        /// <returns></returns>
        public bool Login(string uid, string pwd,bool rememberme) {

            if (CurrentUser.CheckLogin(uid, pwd))
            {
                IsLogin = true;
                
                CurrentUser = User.FindFirst(uid);
                if (CurrentUser != null && CurrentUser.ID != null && HttpContext.Current.Session != null)
                {
                    HttpContext.Current.Session[Const.CurrentUserID] = CurrentUser.ID;
                }
                HttpCookie cookie = new HttpCookie(EnringToken, uid);
                if (rememberme)
                    cookie.Expires = DateTime.Now.AddDays(7.0);
                HttpContext.Current.Response.Cookies.Add(cookie);
                return true;
            }
            else
                return false;
            
        }

        public void Logout()
        {
            IsLogin = false;
            HttpContext.Current.Session.Remove(Const.CurrentUserID);
        }



        private EnringContext( string etcode)
        {
            context = HttpContext.Current;
        }

        private EnringContext()
        {
            context = HttpContext.Current;
            IsLogin = false;
        }
        public static EnringContext Current
        {
            get
            {
                if (encontext == null)
                {
                    string etcode = string.Empty;
                    if (HttpContext.Current.Request.Cookies[EnringToken] != null)
                        etcode = (string)HttpContext.Current.Request.Cookies[EnringToken].Value;
                    if (etcode == string.Empty)
                        encontext = new EnringContext();
                    else
                        encontext = new EnringContext(etcode);                    
                }
                return encontext;
            }
        }

        public static Configration.ConfigrationManager Configration
        {
            get 
            {                
                if(configrationManager == null)
                {
                    //Initialize Configration
                    configrationManager = new Configration.ConfigrationManager(rootPath);
                }
                return configrationManager;
            }
        }
    }
}
